﻿using Basic.UserModel.DB;
using Basic.UserModel.User;
using Basic.UserRemoteModel;
using Basic.UserRemoteModel.User.Model;
using Basic.UserRepository.Linq;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.IdGenerator;
using WeDonekRpc.Model;
using WeDonekRpc.SqlSugar;

namespace Basic.UserRepository.lmpl
{
    internal class UserRepository : UserBaseRepository<DBUserInfo, long>, IUserRepository
    {
        public UserRepository (ISqlClientFactory factory) : base(factory)
        {
        }
        public long RegUser (UserAdd add)
        {
            DateTime now = DateTime.Now;
            DBUserInfo user = new DBUserInfo
            {
                Id = IdentityHelper.CreateId(),
                LastLoginDate = now.Date,
                RegMode = add.RegMode,
                NickName = add.NickName,
                Pwd = add.Pwd,
                RegTime = now,
                Sex = add.Sex,
                UserHead = add.UserHead,
                UserState = UserState.启用
            };
            DBLoginUser ac = new DBLoginUser
            {
                Id = IdentityHelper.CreateId(),
                LoginName = add.LoginName,
                LoginType = add.LoginType,
                UserId = user.Id
            };
            ISqlQueue<DBUserInfo> queue = base.BeginQueue();
            queue.Insert(user);
            queue.Insert(ac);
            _ = queue.Submit();
            return user.Id;
        }


        public void SetPwd (long userId, string pwd)
        {
            if (!base.Update(a => a.Pwd == pwd, a => a.Id == userId))
            {
                throw new ErrorException("user.pwd.update.fail");
            }
        }
        public Result[] Query<Result> (UserQueryParam where, IBasicPage paging, out int count) where Result : class, new()
        {
            paging.InitOrderBy("Id", true);
            return base.Query<Result>(where.ToWhere(), paging, out count);
        }
        public void SetState (long id, UserState state)
        {
            if (!base.Update(new DBUserInfo
            {
                Id = id,
                UserState = state,
                DisableTime = state == UserState.禁用 ? DateTime.Now : null,
            }, new string[]
            {
                "DisableTime",
                "UserState"
            }))
            {
                throw new ErrorException("user.pwd.update.fail");
            }
        }

        public void SetDatum (long id, UserSet datum)
        {
            if (!base.Update(new DBUserInfo
            {
                Id = id,
                NickName = datum.NickName,
                Birthday = datum.Birthday,
                Sex = datum.Sex,
                UserHead = datum.UserHead
            }, new string[]
           {
                "NickName",
                "Birthday",
                "Sex",
                "UserHead"
           }))
            {
                throw new ErrorException("user.datum.update.fail");
            }
        }

        public void SetLastLoginDate (long[] userId)
        {
            if (!base.Update(a => a.LastLoginDate == HeartbeatTimeHelper.CurrentDate, a => userId.Contains(a.Id)))
            {
                throw new ErrorException("user.loginTime.update.fail");
            }
        }
    }
}
